<!-- 会员列表 -->
<template>
  <div class="memberIntegral sys-content">
    <page-mod>
      <el-card>
        <div class="query-box">
          <el-form :model="query" inline>
            <el-form-item label="会员名称:">
              <el-input v-model="query.memberName" placeholder="请输入会员名称" clearable/>
            </el-form-item>
            <el-form-item label="会员编号:">
              <el-input v-model="query.memberCode" placeholder="请输入会员编号" clearable/>
            </el-form-item>
            <el-form-item label="手机号:">
              <el-input v-model="query.phone" placeholder="请输入手机号" clearable/>
            </el-form-item>
          </el-form>
          <div class="btns">
            <el-button icon="Search" color="#16b777" @click="load(1)">查询</el-button>
            <el-button icon="Refresh" color="#FF5722" @click="resetSearch">重置</el-button>
          </div>
        </div>
      </el-card>
    </page-mod>
    <page-mod title="会员积分数据" icon="Medal" :showBtn="false" :padBottom="false">
      <el-card v-loading="loading" element-loading-text="拼命加载中">
        <el-table ref="table" :data="dataList" max-height="493" border>
          <el-table-column label="序号" type="index" align="center" width="60"/>
          <el-table-column label="会员编号" prop="memberCode" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column label="会员名称" prop="memberName" align="center"></el-table-column>
          <el-table-column label="类型" prop="enableStatus" align="center">
            <template #default="{row}">
              <span v-if="row.memberType === 1">普通用户</span>
              <span v-else-if="row.memberType === 2">推广员</span>
              <span v-else>推广员+经销商</span>
            </template>
          </el-table-column>
          <el-table-column label="总积分" prop="totalPoint" align="center"></el-table-column>
          <el-table-column label="可用积分" prop="usablePoint" align="center"></el-table-column>
          <el-table-column label="已用积分" prop="usedPoint" align="center"></el-table-column>
          <el-table-column label="未入账积分" prop="unrecordedPoint" align="center"></el-table-column>
          <el-table-column label="操作" align="center" width="240">
            <template #default="{row}">
              <el-button type="primary" icon="Edit" link @click="toViewTradeInfo(row.memberId, true)">交易日志</el-button>
              <el-divider direction="vertical" />
              <el-button type="primary" icon="Document" link @click="toViewTradeInfo(row.memberId, false)">交易记录</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination 
          class="paging"
          v-show="query.total"
          small
          layout="total, sizes, prev, pager, next, jumper" 
          :current-page="query.current" 
          :page-size="query.size"
          :total="query.total"
          @current-change="load"
          @size-change="sizeChange">
        </el-pagination>
      </el-card>
    </page-mod>

    <!-- 交易日志,交易记录弹窗 -->
    <el-dialog 
      v-model="show" 
      :title="tradeType ? '交易日志' : '交易记录'" 
      :width="tradeType ? '90%' : '60%'" 
      top="10vh" 
      center 
      draggable 
      @close="dialogClose">
      <el-table ref="table" :data="tradeList" max-height="493" border v-loading="loading1" element-loading-text="拼命加载中">
        <el-table-column label="序号" type="index" align="center" width="60"/>
        <el-table-column label="操作时间" prop="memberCode" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column label="业务编号" prop="totalPoint" align="center"></el-table-column>
        <el-table-column label="业务类型" prop="enableStatus" align="center">
          <!-- <template #default="{row}">
            <span v-if="row.memberType === 1">普通用户</span>
            <span v-else-if="row.memberType === 2">推广员</span>
            <span v-else>推广员+经销商</span>
          </template> -->
        </el-table-column>
        <el-table-column v-if="tradeType" label="交易积分数" prop="memberName" align="center"></el-table-column>
        <el-table-column v-if="tradeType" label="盈亏类型" prop="enableStatus" align="center">
          <!-- <template #default="{row}">
            <span v-if="row.memberType === 1">普通用户</span>
            <span v-else-if="row.memberType === 2">推广员</span>
            <span v-else>推广员+经销商</span>
          </template> -->
        </el-table-column>
        <el-table-column v-if="tradeType" label="总积分" prop="usablePoint" align="center"></el-table-column>
        <el-table-column v-if="tradeType" label="交易后总积分" prop="usablePoint" align="center"></el-table-column>
        <el-table-column v-if="tradeType" label="已使用积分" prop="usablePoint" align="center"></el-table-column>
        <el-table-column v-if="tradeType" label="交易后已使用积分" prop="usedPoint" align="center"></el-table-column>
        <el-table-column v-if="tradeType" label="可用积分" prop="unrecordedPoint" align="center"></el-table-column>
        <el-table-column v-if="tradeType" label="交易后可用积分" prop="unrecordedPoint" align="center"></el-table-column>
        <el-table-column v-if="tradeType" label="未入账积分" prop="unrecordedPoint" align="center"></el-table-column>
        <el-table-column v-if="tradeType" label="交易后未入账积分" prop="unrecordedPoint" align="center"></el-table-column>
        <el-table-column v-if="tradeType" label="说明" prop="unrecordedPoint" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column v-if="!tradeType" label="购买人手机" prop="unrecordedPoint" align="center"></el-table-column>
        <el-table-column v-if="!tradeType" label="记录说明" prop="unrecordedPoint" align="center" show-overflow-tooltip></el-table-column>
        <el-table-column v-if="!tradeType" label="交易积分数" prop="unrecordedPoint" align="center"></el-table-column>
        <el-table-column v-if="!tradeType" label="交易状态" prop="unrecordedPoint" align="center"></el-table-column>
      </el-table>
      <el-pagination 
        class="paging"
        v-show="query1.total"
        small
        layout="total, sizes, prev, pager, next, jumper" 
        :current-page="query1.current" 
        :page-size="query1.size"
        :total="query1.total"
        @current-change="loadTrade"
        @size-change="tradeSizeChange">
      </el-pagination>
    </el-dialog>
  </div>
</template>

<script setup name="memberIntegral">
  import { getMemberIntegralPagesApi, getMiTradeLogPagesApi, getMiTradeRecordPagesApi } from '@/api/api'
  import { reactive, toRefs, getCurrentInstance, onActivated } from 'vue'

  // 获取注册的全局实列
  const currentThis = getCurrentInstance().appContext.config.globalProperties

  const currentData = reactive({
    loading: false,
    loading1: false,
    show: false,
    tradeType: false,
    query: {
      memberName: '',
      memberCode: '',
      phone: '',
      current: 1,
      size: 20,
      total: 0
    },
    query1: {
      memberId: '',
      current: 1,
      size: 20,
      total: 0
    },
    dataList: [],
    tradeList: []
  })
  let {loading, loading1, show, tradeType, query, query1, dataList, tradeList} = toRefs(currentData)

  // 查询
  function load(page) {
    currentData.loading = true
    currentData.query.current = page
    getMemberIntegralPagesApi(currentData.query).then(res => {
      currentData.loading = false
      if(res.code === '200') {
        currentData.dataList = res.data.pages
        currentData.query.total = res.data.total
      } else {
        currentThis.$message.error(res.message)
      }
    })
  }

  // 查询交易日志或交易记录
  function loadTrade(page) {
    currentData.loading1 = true
    currentData.query1.current = page
    if(currentData.tradeType) {
      getMiTradeLogPagesApi(currentData.query1).then(res => { // 查询交易日志
        currentData.loading1 = false
        if(res.code === '200') {
          currentData.tradeList = res.data.pages
          currentData.query1.total = res.data.total
        } else {
          currentThis.$message.error(res.message)
        }
      })
    } else {
      getMiTradeRecordPagesApi(currentData.query1).then(res => { // 查询交易记录
        currentData.loading1 = false
        if(res.code === '200') {
          currentData.tradeList = res.data.pages
          currentData.query1.total = res.data.total
        } else {
          currentThis.$message.error(res.message)
        }
      })
    }
  }

  // 点击交易日志或交易记录
  function toViewTradeInfo(memberId, type) {
    this.tradeType = type
    this.query1.memberId = memberId
    this.loadTrade(1)
    this.show = true
  }

  // 重置查询
  function resetSearch() {
    currentData.query.memberName = ''
    currentData.query.memberCode = ''
    currentData.query.phone = ''
    load(1)
  }

  // 交易日志,交易记录弹窗关闭时候重置查询条件
  function dialogClose() {
    currentData.query1 = {
      memberId: '',
      current: 1,
      size: 20,
      total: 0
    }
    currentData.tradeList = []
  }

  // 分页每页显示条数改变执行
  function sizeChange(size) {
    currentData.query.size = size
    load(1)
  }

  // 交易日志和交易记录弹窗分页每页显示条数改变执行
  function tradeSizeChange(size) {
    currentData.query.size = size
    loadTrade(1)
  }

  onActivated(() => {
    load(currentData.query.current)
  })
</script>

<style lang="less" scoped>
.memberIntegral{
  :deep(.el-table) {
    .img-cell{
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>